package com.erps.CommonUtils;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;

public class ExportWord {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		ExportWord cfg = new ExportWord();
		cfg.ExportReport("D:\\report.doc");
	}

	public HWPFDocument getDocument(String cFileName) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(new File(cFileName));
			HWPFDocument doc;
			try {
				doc = new HWPFDocument(fis);
				return doc;
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void ExportReport(String cFileName) {
		try {
			HWPFDocument doc = this.getDocument("d:\\empty.doc");
			Range range = doc.getRange();
			Map<String, String> v1 = new HashMap<String, String>();
			v1.put("{title}", "关于电子政务系统的培训！");
			HWPFDocument A1 = LoadTemplate("d:\\template1.doc", v1);
			Copy(doc, A1);
			Map<String, String> v2 = new HashMap<String, String>();
			v2.put("{title}", "第一篇");
			v2.put("{yyyy-MM-dd}", "2013-10-21");
			v2.put("{body}", "正文");
			HWPFDocument A2 = LoadTemplate("d:\\template2.doc", v2);
			Copy(doc, A2);
			FileOutputStream f = new FileOutputStream(cFileName);
			doc.write(f);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	public void Copy(HWPFDocument doc, HWPFDocument docCopy) {
		Range range = doc.getRange();
		Range BodyRange = docCopy.getRange();
		int iPart = BodyRange.numParagraphs();
		for (int i = 0; i < iPart; i++) {
			Paragraph p = BodyRange.getParagraph(i);
			CharacterProperties cp = new CharacterProperties();
			cp.setBold(true);
			cp.setColor(Color.RED.getRGB());
			cp.setCharacterSpacing(10);
			cp.setChse(cp.SPRM_CHARSCALE);
			cp.setCapitalized(true);
			range.insertAfter(p.text(), cp);
		}
	}

	public HWPFDocument LoadTemplate(String cFileName, Map<String, String> arKeys) {
		HWPFDocument doc = this.getDocument(cFileName);
		for (Map.Entry<String, String> m : arKeys.entrySet()) {
			String cKey = m.getKey();
			String cValue = m.getValue();
			ReplaceKeyWord(doc, cKey, cValue);
		}
		return doc;
	}

	public void ReplaceKeyWord(HWPFDocument doc, String cKeyWord, String cStr) {
		Range bodyRange = doc.getRange();
		bodyRange.replaceText(cKeyWord, cStr);
	}

	public void ReplaceKeyWord(HWPFDocument doc, Map<String, String> arKeys) {
		for (Map.Entry<String, String> m : arKeys.entrySet()) {
			String cKey = m.getKey();
			String cValue = m.getValue();
			ReplaceKeyWord(doc, cKey, cValue);
		}
	}

	public void WriteJ(HWPFDocument doc) {
		try {
			Range range = doc.getRange();
			// 得到文档的读取范围
			TableIterator it = new TableIterator(range);
			// 迭代文档中的表格
			while (it.hasNext()) {
				Table tb = (Table) it.next();
				// 迭代行，默认从0开始

				for (int i = 0; i < tb.numRows(); i++) {
					TableRow tr = tb.getRow(i);
					// 迭代列，默认从0开始
					for (int j = 0; j < tr.numCells(); j++) {
						TableCell td = tr.getCell(j);
						// 取得单元格
						// 取得单元格的内容
						for (int k = 0; k < td.numParagraphs(); k++) {
							Paragraph para = td.getParagraph(k);
							String s = para.text();
							System.out.println(s);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}